package com.ruoyi.adapaySettleaccount.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.huifu.adapay.core.exception.BaseAdaPayException;
import com.huifu.adapay.model.SettleAccount;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.adapaySettleaccount.mapper.AdapaySettleaccountMapper;
import com.ruoyi.adapaySettleaccount.domain.AdapaySettleaccount;
import com.ruoyi.adapaySettleaccount.service.IAdapaySettleaccountService;

/**
 * 汇付结算账户Service业务层处理
 *
 * @author ch
 * @date 2022-02-22
 */
@Service
public class AdapaySettleaccountServiceImpl implements IAdapaySettleaccountService
{
    @Autowired
    private AdapaySettleaccountMapper adapaySettleaccountMapper;
    @Autowired
    private ISysConfigService configService;
    /**
     * 查询汇付结算账户
     *
     * @param id 汇付结算账户主键
     * @return 汇付结算账户
     */
    @Override
    public AdapaySettleaccount selectAdapaySettleaccountById(Long id)
    {
        return adapaySettleaccountMapper.selectAdapaySettleaccountById(id);
    }

    /**
     * 查询汇付结算账户列表
     *
     * @param adapaySettleaccount 汇付结算账户
     * @return 汇付结算账户
     */
    @Override
    public List<AdapaySettleaccount> selectAdapaySettleaccountList(AdapaySettleaccount adapaySettleaccount)
    {
        return adapaySettleaccountMapper.selectAdapaySettleaccountList(adapaySettleaccount);
    }

    /**
     * 新增汇付结算账户
     *
     * @param adapaySettleaccount 汇付结算账户
     * @return 结果
     */
    @Override
    public int insertAdapaySettleaccount(AdapaySettleaccount adapaySettleaccount)
    {
        adapaySettleaccount.setCreateTime(DateUtils.getNowDate());
        return adapaySettleaccountMapper.insertAdapaySettleaccount(adapaySettleaccount);
    }

    /**
     * 修改汇付结算账户
     *
     * @param adapaySettleaccount 汇付结算账户
     * @return 结果
     */
    @Override
    public int updateAdapaySettleaccount(AdapaySettleaccount adapaySettleaccount)
    {
        adapaySettleaccount.setUpdateTime(DateUtils.getNowDate());
        return adapaySettleaccountMapper.updateAdapaySettleaccount(adapaySettleaccount);
    }

    /**
     * 批量删除汇付结算账户
     *
     * @param ids 需要删除的汇付结算账户主键
     * @return 结果
     */
    @Override
    public int deleteAdapaySettleaccountByIds(Long[] ids)
    {
        return adapaySettleaccountMapper.deleteAdapaySettleaccountByIds(ids);
    }


    /**
     * 批量逻辑删除汇付结算账户
     *
     * @param ids 需要删除的汇付结算账户主键
     * @return 结果
     */
    @Override
    public int logicDeleteAdapaySettleaccount(Long[] ids,String delType,Long delUser)
    {
        return adapaySettleaccountMapper.logicDeleteAdapaySettleaccount(ids,delType,delUser);
    }

    /**
     * 删除汇付结算账户信息
     *
     * @param id 汇付结算账户主键
     * @return 结果
     */
    @Override
    public int deleteAdapaySettleaccountById(Long id)
    {
        return adapaySettleaccountMapper.deleteAdapaySettleaccountById(id);
    }
    public Map<String, Object> createSettleAccount(String member_id, String cardId, String cardName, String certId, String phone){
        String app_id=configService.selectConfigByKey("adpay_app_id");
        Map<String, Object> settleCountParams = new HashMap<String, Object>();
        Map<String, Object> accountInfo = new  HashMap<String, Object>();
        accountInfo.put("card_id",cardId);
        accountInfo.put("card_name",cardName);
        accountInfo.put("cert_id",certId);
        accountInfo.put("cert_type","00");
        accountInfo.put("tel_no",phone);
//        accountInfo.put("bank_code","03060000");
        accountInfo.put("bank_acct_type","2");
//        accountInfo.put("prov_code","0031");
//        accountInfo.put("area_code","3100");
        settleCountParams.put("member_id",member_id);
        settleCountParams.put("app_id", app_id);
        settleCountParams.put("channel","bank_account");
        settleCountParams.put("account_info", accountInfo);
        Map<String, Object> settleCount = null;
        try {
            settleCount = SettleAccount.create(settleCountParams);
        } catch (BaseAdaPayException e) {
            e.printStackTrace();
        }
        return settleCount;
    }
    public Map<String, Object> delSettleAccount(String member_id,String settle_account_id){
        String app_id=configService.selectConfigByKey("adpay_app_id");
        Map<String, Object> settleCountParams = new  HashMap<String, Object>();
        settleCountParams.put("settle_account_id", settle_account_id);
        settleCountParams.put("member_id", member_id);
        settleCountParams.put("app_id", app_id);
        Map<String, Object> settleCount=null;
        try {

            settleCount = SettleAccount.delete(settleCountParams);

        } catch (BaseAdaPayException e) {
            e.printStackTrace();
        }
        return settleCount;
    }
}
